<template>
  <div class="app-container">

    <div class="title-btns">
      <span class="role-span"><span></span>在线用户</span>
    </div>
    <!--表格渲染-->
    <el-table v-loading="loading" :data="data" size="small" style="width: 100%;">
      <el-table-column prop="user_id" label="用户ID" />
      <el-table-column prop="user_name" label="用户名称" />
      <el-table-column prop="client_id" label="应用标识" />
      <el-table-column prop="token_type" label="类型" />
      <el-table-column prop="token_value" label="token" />
      <el-table-column prop="grant_type" label="授权方式" />
      <el-table-column v-if="checkPermission(['ADMIN','TOKEN_ALL','TOKEN_REMOVE'])" label="操作" width="130px" align="center">
        <template slot-scope="scope">
          <el-popover
            :ref="scope.row.token_value"
            placement="top"
            width="180"
          >
            <p>确定下线此用户吗？</p>
            <div style="text-align: right; margin: 0">
              <el-button size="mini" type="text" @click="$refs[scope.row.token_value].doClose()">取消</el-button>
              <el-button :loading="delLoading" type="primary" size="mini" @click="subDelete(scope.row.token_value)">确定</el-button>
            </div>
            <el-button slot="reference" type="text" size="mini">下线</el-button>
          </el-popover>
        </template>
      </el-table-column>
    </el-table>

    <!--分页组件-->
    <el-pagination
      :total="total"
      :current-page="page"
      style="margin-top: 8px;"
      layout="total, sizes, prev, pager, next, jumper"
      @size-change="sizeChange"
      @current-change="pageChange"
    />

  </div>
</template>

<script>
import checkPermission from '@/utils/permission'
import initData from '@/mixins/initData'
import initDict from '@/mixins/initDict'
import { remove } from '@/api/auth/token'
import { parseTime } from '@/utils/index'

export default {
  mixins: [initData, initDict],
  data() {
    return {
      delLoading: false, sup_this: this
    }
  },
  created() {
    this.$nextTick(() => {
      this.init()
      // 加载数据字典
      this.getDict('remove_status')
    })
  },
  methods: {
    parseTime,
    checkPermission,
    beforeInit() {
      this.url = 'oauth/token/list'
      this.params = { page: this.page, size: this.size }
      return true
    },
    subDelete(token) {
      this.delLoading = true
      remove(token).then(res => {
        this.delLoading = false
        this.$refs[token].doClose()
        this.init()
        this.$notify({
          title: '删除成功',
          type: 'success',
          duration: 2500
        })
      }).catch(err => {
        this.delLoading = false
        this.$refs[token].doClose()
        console.log(err.response.data.msg)
      })
    }
  }
}
</script>

<style scoped>

</style>
